<template>
  <div class="add-box">
    <el-dialog width="600px" top="5%" :title="dialogTitle" v-model="dialogFormVisible" :close-on-click-modal="false"
      :draggable="true" @close="dialogClose">
      <div>
        <el-form ref="dialogFormRef" :model="dialogForm" :rules="dialogFormRules">
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="商户" prop="shopId">
                <el-select :disabled="dialogType == 'edit'" filterable style="width:100%" v-model="dialogForm.shopId"
                  placeholder="请选择商户">
                  <el-option v-for="item in shopOptions" :key="item.shopId" :label="item.shopName" :value="item.shopId" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="第三方平台" prop="operatorCode">
                <el-select :disabled="dialogType == 'edit'" style="width:100%" v-model="dialogForm.operatorCode"
                  @change="codeChange">
                  <el-option v-for="item in sanOptions" :key="item.operatorCode" :label="item.operatorName"
                    :value="item.operatorCode" />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="otherAppId" prop="otherAppId">
                <el-input v-model="dialogForm.otherAppId" maxlength="32" placeholder="请输入otherAppId"
                  @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="otherAppPwd" prop="otherAppPwd">
                <el-input v-model="dialogForm.otherAppPwd" placeholder="请输入otherAppPwd" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="otherPukKey" prop="otherPukKey">
                <el-input v-model="dialogForm.otherPukKey" placeholder="请输入otherPukKey" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="平台请求地址" prop="otherReqUrl">
                <el-input v-model="dialogForm.otherReqUrl" placeholder="请输入平台请求地址" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="支付金额" prop="price">
                <el-input type="number" v-model="dialogForm.price" placeholder="请输入支付金额">
                  <template #append>元</template>
                </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item style="width:90%;" label="状态" prop="state">
                <el-radio-group v-model="dialogForm.state">
                  <el-radio :label="2">待支付</el-radio>
                  <el-radio :label="3">已支付</el-radio>
                  <el-radio :label="4">冻结</el-radio>
                </el-radio-group>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="备注" prop="remarks">
                <el-input type="textarea" v-model="dialogForm.remarks" placeholder="请输入备注" @keypress.enter="submitHandle" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="submitHandle">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
    <confirm-pass ref="confirmPassRef" @verification="postData" />
  </div>
</template>

<script>
import { reactive, ref, toRefs, watch } from 'vue'
import { successDeal, warningDeal } from '@/utils/utils'
import confirmPass from '@/components/confirmPass.vue'
import api from '../api'
export default {
  components: {
    confirmPass
  },
  setup(props, context) {
    const dialogFormRef = ref(null)
    const confirmPassRef = ref(null)

    const state = reactive({
      dialogTitle: '',
      dialogType: null,
      dialogFormVisible: false,
      incomingForm: {},
      dialogForm: {
        state: 2,
      },
      dialogFormRules: {
        otherReqUrl: [
          { required: true, message: '请输入平台请求地址', trigger: 'blur' },
        ],
        shopId: [
          { required: true, message: '请选择商户', trigger: 'blur' },
        ],
        operatorCode: [
          { required: true, message: '请选择第三方平台', trigger: 'blur' },
        ],
        price: [
          { required: true, message: '请输入支付金额', trigger: 'blur' },
          {
            pattern: /^[0-9]+(.[0-9]{1,2})?$/, message: '请输入最多两位小数的正数', trigger: 'blur'
          }
        ],
      },
      sanOptions: [],
      shopOptions: [],
    })

    watch(() => state.dialogType, (newVal, oldVal) => {
      if (newVal == 'add') {
        state.dialogTitle = '新增自动生产接口'
      } else if (newVal == 'edit') {
        state.dialogTitle = '编辑自动生产接口'
      }
    })

    function dialogShow(type, data) {
      state.dialogType = type
      if (type == 'edit') {
        state.dialogForm = { ...data }
        state.dialogForm.price = data.price / 100
      }
      state.incomingForm = data
      getQryList()
      getShopList()
      state.dialogFormVisible = true
    }

    function getShopList() {
      const obj = {
        page: 1,
        size: 10000,
      }
      api.shopList(obj).then(res => {
        if (res.code == '200') {
          state.shopOptions = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function dialogClose() {
      state.dialogForm = {
        state: 2,
      }
      dialogFormRef.value.resetFields()
    }

    function submitHandle() {
      dialogFormRef.value.validate((valid, fields) => {
        if (valid) {
          if (state.dialogType == 'add') {
            const title = '自动生产接口新增提示'
            const msg = '是否确认新增自动生产接口？'
            confirmPassRef.value.dialogShow(title, msg)
          } else {
            const title = '自动生产接口修改提示'
            const msg = '是否确认修改自动生产接口？'
            confirmPassRef.value.dialogShow(title, msg)
          }
        }
      })
    }

    function postData() {
      const obj = {
        ...state.dialogForm,
        price: state.dialogForm.price * 100 || '',
      }
      if (state.dialogType == 'add') {
        api.apiUserAdd(obj).then(res => {
          if (res.code == '200') {
            successDeal(res.msg)
            context.emit('dialogClose', true)
            state.dialogFormVisible = false
          } else {
            warningDeal(res.msg)
          }
        }).catch(err => {
          console.log(err)
        })
      } else {
        api.apiUserModify(obj).then(res => {
          if (res.code == '200') {
            successDeal(res.msg)
            context.emit('dialogClose', true)
            state.dialogFormVisible = false
          } else {
            warningDeal(res.msg)
          }
        }).catch(err => {
          console.log(err)
        })
      }
    }

    function getQryList() {
      const obj = {
        page: 1,
        size: 10000,
      }
      api.qryList(obj).then(res => {
        if (res.code == '200') {
          state.sanOptions = res.data.list
        } else {
          warningDeal(res.msg)
        }
      }).catch(err => {
        console.log(err)
      })
    }

    function getUrl(operatorCode) {
      let otherReqUrl = ''
      state.sanOptions && state.sanOptions.map(item => {
        if (item.operatorCode == operatorCode) {
          otherReqUrl = item.otherReqUrl
        }
      })
      return otherReqUrl
    }

    function codeChange(e) {
      state.dialogForm.otherReqUrl = getUrl(e)
    }

    return {
      ...toRefs(state),
      dialogFormRef,
      confirmPassRef,
      dialogShow,
      submitHandle,
      dialogClose,
      postData,
      codeChange,
    }
  }
}
</script>

<style lang="scss" scoped>
:deep(.el-input) {
  .el-input-group__append {
    padding: 0 20px !important;
  }
}
</style>